﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AssessmentTool.Controls;
using AssessmentTool.Services.DTO;
using ModelLayer;
using AssessmentTool.ServiceBridge;
using AssessmentTool.PresentationLogic.EventArguments;
using AssessmentTool.Services;


namespace AssessmentTool.PresentationLogic.Controllers
{
    public class LoginController : AbstractController<Login>
    {
        
        public LoginController(Login element)
            : base(element)
        {
            
            ed.subscrive("LOGIN", element.onLogin);
            ed.subscrive("LOGOUT", element.onLogout);

        }

        public void callLogin()
        {

            UtenteServiceBridge.login(element.txtUsername.Text, element.txtPassword.Password, loginCompleted);

        }
        protected virtual bool isAutorizzato(String ruolo)
        {
            return ruolo.CompareTo("UTENTE") == 0;
        }


        void loginCompleted(object sender, loginCompletedEventArgs e)
        {

            if (e.Error!=null)
            {
                ed.dispatch("STOPBUSY", new EventArgs());
                ed.dispatch("SHOWMESSAGE", new ShowMessageEventArgs() { Testo="Errore critico nella richiesta...",Critico=true,Error=true });
                
            }
            else
            {
                UtenteLogged u = e.Result;
                if (u != null)
                {
                    if (isAutorizzato(u.utente.Gruppo.Id))
                    {
                        Session.getInstance().addValue("USER", u.utente);
                        Session.getInstance().addValue("USERLOGGED", u);
                        ModelloServiceBridge.getDictionary(u.utente.Compagnia.Lingua, dictionaryDownloaded);
                        return;
                    }
                }
                
                ed.dispatch("STOPBUSY", new EventArgs());
                element.ErrorLogin.Begin();

                
                
            }
           
        }
        void dictionaryDownloaded(object sender, getDictionaryCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                ed.dispatch("STOPBUSY", new EventArgs());
                ed.dispatch("SHOWMESSAGE", new ShowMessageEventArgs() { Testo = "Impossibile caricare il dizionario per la lingua richiesta!", Critico = true, Error = true });

            }
            else
            {
                ed.dispatch("STOPBUSY", new EventArgs());
                Session.getInstance().setLanguageDictionary(e.Result);
                launchLogin();
            }
        }

        protected virtual void launchLogin()
        {
            ed.dispatch("LOGIN", new EventArgs());
        }

    }
}
